package com.todod.base.module.system.dal.mysql.dict;

import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.mybatis.core.mapper.BaseMapperX;
import com.todod.base.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.todod.base.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import com.todod.base.module.system.dal.dataobject.dict.DictDataDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Mapper
public interface DictDataMapper extends BaseMapperX<DictDataDO> {

    default DictDataDO selectByDictTypeAndValue(String dictType, String value) {
        return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getValue, value);
    }

    default DictDataDO selectByDictTypeAndLabel(String dictType, String label) {
        return selectOne(DictDataDO::getDictType, dictType, DictDataDO::getLabel, label);
    }

    default List<DictDataDO> selectByDictTypeAndValues(String dictType, Collection<String> values) {
        return selectList(new LambdaQueryWrapper<DictDataDO>().eq(DictDataDO::getDictType, dictType)
                .in(DictDataDO::getValue, values));
    }

    default long selectCountByDictType(String dictType) {
        return selectCount(DictDataDO::getDictType, dictType);
    }

    default PageResult<DictDataDO> selectPage(DictDataPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DictDataDO>()
                .likeIfPresent(DictDataDO::getLabel, reqVO.getLabel())
                .eqIfPresent(DictDataDO::getDictType, reqVO.getDictType())
                .eqIfPresent(DictDataDO::getStatus, reqVO.getStatus())
                .orderByDesc(Arrays.asList(DictDataDO::getDictType, DictDataDO::getSort)));
    }

    default List<DictDataDO> selectListByStatusAndDictType(Integer status, String dictType) {
        return selectList(new LambdaQueryWrapperX<DictDataDO>()
                .eqIfPresent(DictDataDO::getStatus, status)
                .eqIfPresent(DictDataDO::getDictType, dictType));
    }

    /**
     *通过父级获取所有子级以及子级数据
     **/
    @Select("SELECT wd.sort ,wd.label,wd.`value`,wd.dict_type " +
            "FROM system_dict_type wt " +
            "INNER JOIN system_dict_data wd " +
            "ON wt.type = wd.dict_type " +
            "WHERE wt.type IN (" +
            "    SELECT nd.`value` " +
            "    FROM system_dict_data nd " +
            "    WHERE nd.dict_type = 'yq_type' " +
            "    AND nd.`status` = 0 AND nd.deleted = 0 " +
            ") AND wt.`status` = 0 AND wt.deleted = 0")
    public List<DictDataDO> relationalDict(String dictType);


    /**
     * 获取父级字典数据列表
     */
    @Select("SELECT " +
            "   sort,label,`value` " +
            "FROM " +
            "  system_dict_data " +
            "WHERE " +
            "  dict_type = #{dictType} " +
            "  AND `status` = 0  " +
            "  AND deleted = 0")
    public List<DictDataDO> getdictType(String dictType);

    /**
     * 获取父级字典下对应类型子级字典数据列表
     */
    @Select("SELECT " +
            "  wd.sort, " +
            "  wd.label, " +
            "  wd.`value`, " +
            "  wd.dict_type " +
            "FROM " +
            "  system_dict_type wt " +
            "  INNER JOIN system_dict_data wd ON wt.type = wd.dict_type " +
            "WHERE " +
            "  wt.type = #{Type} " +
            "  AND wt.`status` = 0 " +
            "  AND wt.deleted = 0")
    public List<DictDataDO> getdictData(String Type);


    /**
     * 获取仪器字典型号(字典为qy_model)
     */

    @Select("SELECT t.label,t.`value` FROM system_dict_data t " +
            "WHERE t.label LIKE '%${params}%' " +
            "  AND t.dict_type = 'qy_model'  AND t.`status`=0 AND t.deleted = 0")
    public List<DictDataDO> getDeviceMode(String params);

    /**
     * 获取仪器字典型号(字典为yq_type)
     */
    @Select("SELECT " +
            "  wd.sort, " +
            "  wd.label, " +
            "  wd.`value`, " +
            "  wd.dict_type " +
            "FROM" +
            "  system_dict_type wt" +
            "  INNER JOIN system_dict_data wd ON wt.type = wd.dict_type " +
            "WHERE" +
            "  wt.type IN ( SELECT nd.`value` FROM system_dict_data nd WHERE nd.dict_type = 'yq_type' AND nd.`status` = 0 AND nd.deleted = 0 ) " +
            "  AND (wd.`value`LIKE '%${params}%' OR wd.label LIKE '%${params}%') " +
            "  AND wt.`status` = 0 " +
            "  AND wt.deleted = 0;")
    public List<DictDataDO> getDeviceModeNew(String params);


}
